package com.situ.project1.controller;

import com.github.pagehelper.PageInfo;
import com.situ.project1.pojo.Car;
import com.situ.project1.pojo.Employee;
import com.situ.project1.pojo.search.CarSearchModel;
import com.situ.project1.service.CarService;
import com.situ.project1.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author:yzy
 * @date:2024/8/10
 */
@RestController
@RequestMapping("/api/car")
public class CarController {
    private CarService carService;
    @Autowired
    public CarController(CarService carService) {
        this.carService = carService;
    }
    @GetMapping
    public Map<String,Object> findAll(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            CarSearchModel car
    ){
        if (pageNo==null||pageNo<1){
            pageNo=1;
        }
        if (pageSize==null||pageSize<1){
            pageSize=10;
        }
        PaginateInfo pi =new PaginateInfo(pageNo,pageSize);
        List<Car> cars = carService.findAll(pi,car);

        PageInfo pif = new PageInfo<>(cars);
        Map<String,Object> map = Map.of("total",pif.getTotal(),"pageNo",pif.getPageNum(),"pageSize",pif.getPageSize());
        return Map.of("success",true,"rows",cars,"pi",map);
    }
//    查询剩余数量大于一
    @GetMapping("/rest")
    public Map<String,Object> findRest(){
        List<Car> cars = carService.findRest();
        return Map.of("success",true,"rows",cars);
    }

    @PostMapping
    public Map<String,Object> add(@RequestBody Car car){
        if (
                car.getModel()==null||
                car.getModel().trim().length()==0||
                car.getPicture()==null||
                car.getPicture().trim().length()==0||
                car.getAdvantage()==null||
                car.getAdvantage().trim().length()==0||
                car.getFreeCount()==null||
                car.getOrderCount()==null
        ){
            return Map.of("success",false);
        }else {
            boolean b = carService.add(car);
            return Map.of("success",b);
        }

    }
    @PutMapping
    public Map<String,Object> update(@RequestBody Car car){
        boolean b = carService.update(car);
        return Map.of("success",b);
    }
    @DeleteMapping
    public Map<String,Object> delete(@RequestBody Integer ids[]){
        boolean b = carService.delete(ids);
        return Map.of("success",b);
    }
    @GetMapping("/restCar")
    public Map<String,Object> findRest(@RequestParam LocalDateTime startTime){
        LocalDateTime endTime = startTime.plusHours(1);
        List<Car> cars = carService.findRestCar(startTime,endTime);
        System.out.println(cars);
        return Map.of("success",true,"rows",cars);
    }

}
